package com.example.testappwidget.ui.activity

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentContainerView
import androidx.fragment.app.FragmentTransaction
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.slidingpanelayout.widget.SlidingPaneLayout
import com.example.testappwidget.R
import com.example.testappwidget.adpter.SlidingPaneLeftMenuAdapter
import com.example.testappwidget.ui.fragment.SelectAnItemFragment


/**
 *Author: chinadragon
 *Time: 2024/7/8
 */
class SlidingPaneLayoutActivity : AppCompatActivity() {
    private lateinit var sliding_pane_layout: SlidingPaneLayout
    private lateinit var rv_pane: RecyclerView
    private lateinit var detail_container: FragmentContainerView
    private lateinit var mSlidingPaneLeftMenuAdapter: SlidingPaneLeftMenuAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_slidingpanelayout)
        initView()
        initData()
    }

    private fun initView() {
        sliding_pane_layout = findViewById(R.id.sliding_pane_layout)
        rv_pane = findViewById(R.id.rv_pane)
        detail_container = findViewById(R.id.detail_container)
    }

    private fun initData() {
        val data = listOf("android", "kotlin", "vue", "ios", "typescript")
        mSlidingPaneLeftMenuAdapter = SlidingPaneLeftMenuAdapter(this) {
            openDetails(it)
        }
        mSlidingPaneLeftMenuAdapter.setData(data)
        rv_pane.apply {
            layoutManager = LinearLayoutManager(this@SlidingPaneLayoutActivity)
            adapter = mSlidingPaneLeftMenuAdapter

        }
    }

    // A method on the Fragment that owns the SlidingPaneLayout,called by the
// adapter when an item is selected.
    @SuppressLint("CommitTransaction")
    fun openDetails(itemId: Int) {
        val bundle = Bundle()
        bundle.putString(SelectAnItemFragment.KEY_CONTENT, "$itemId")
        supportFragmentManager.beginTransaction()
            .setReorderingAllowed(true)
            .replace(R.id.detail_container, SelectAnItemFragment::class.java, bundle)
            .commit()

        if (sliding_pane_layout.isOpen) {
            supportFragmentManager.beginTransaction()
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
        }
        sliding_pane_layout.openPane()
    }
}